package com.linkage.seapeak.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.activity.Project.ProTalkDetailActivity;
import com.linkage.jz.bankruptcy.base.BaseActivity;
import com.linkage.jz.bankruptcy.fragment.MsgFragment;
import com.linkage.jz.bankruptcy.widget.PublicPopupMenu;
import com.linkage.seapeak.activity.chat.ChatDetailActivity;
import com.linkage.seapeak.adapter.ConversationAdapter;
import com.linkage.seapeak.net.ResultCallBack;
import com.linkage.seapeak.pojo.MessageBundle;
import com.linkage.seapeak.pojo.MessageItem;
import com.seapeak.recyclebundle.AYSwipeRecyclerView;
import com.seapeak.recyclebundle.BaseRecyclerAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

public class ConversationSearchFragment extends BaseNoticeFragment {

    Boolean initData = false;
    int type = 0;
    ArrayList<MessageItem> mDataList;
    private LinearLayout topSlider;
    private AYSwipeRecyclerView recycler;
    private ConversationAdapter adapter;
    private String searchKey;

    public static ConversationSearchFragment newInstance(int type) {
        return new ConversationSearchFragment(type);
    }

    public ConversationSearchFragment() {
    }

    @SuppressLint("ValidFragment")
    public ConversationSearchFragment(int type) {
        this.type = type;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_conversation_list, container, false);
        topSlider = (LinearLayout) view.findViewById(R.id.top_slider);
        recycler = (AYSwipeRecyclerView) view.findViewById(R.id.recycler);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData = true;
        mDataList = new ArrayList<>();
        init();
    }

    private void init() {
        adapter = new ConversationAdapter(getActivity(), type, mDataList);
        recycler.setAdapter(adapter);
        recycler.setMode(AYSwipeRecyclerView.SwipeType.DISABLE);
        recycler.setEmptyView(View.inflate(getActivity(), R.layout.recycle_empty_notice, null));

        topSlider.setVisibility(View.GONE);
        recycler.setOnItemLongClickListener(new BaseRecyclerAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(View v, int position) {
                final MessageItem item = mDataList.get(position);
                if (item != null) {
                    PublicPopupMenu popupMenu;
                    if (!item.isStatus()) {
                        popupMenu = new PublicPopupMenu(getActivity(), "标记已读", "删除", "", "", "");
                        popupMenu.setOnItemClickListener(new PublicPopupMenu.OnItemClickListener() {
                            @Override
                            public void onClick(PublicPopupMenu.MENUITEM m, String str) {
                                if (str.equals("0")) {
                                    showProgress();
                                    readone(item.getId(), new ResultCallBack<String>() {
                                        @Override
                                        public void onSuccess(String result) {
                                            hideProgress();
                                            getData();
                                        }

                                        @Override
                                        public void onFailed(String msg) {
                                            hideProgress();
                                            showToast(msg);
                                        }
                                    });
                                } else if (str.equals("1")) {
                                    showProgress();
                                    deleteMsg(item.getId(), new ResultCallBack<String>() {
                                        @Override
                                        public void onSuccess(String result) {
                                            hideProgress();
                                            getData();
                                        }

                                        @Override
                                        public void onFailed(String msg) {
                                            hideProgress();
                                            showToast(msg);
                                        }
                                    });
                                }
                            }
                        });
                    } else {
                        popupMenu = new PublicPopupMenu(getActivity(), "删除", "", "", "", "");
                        popupMenu.setOnItemClickListener(new PublicPopupMenu.OnItemClickListener() {
                            @Override
                            public void onClick(PublicPopupMenu.MENUITEM m, String str) {
                                showProgress();
                                deleteMsg(item.getId(), new ResultCallBack<String>() {
                                    @Override
                                    public void onSuccess(String result) {
                                        hideProgress();
                                        getData();
                                    }

                                    @Override
                                    public void onFailed(String msg) {
                                        hideProgress();
                                        showToast(msg);
                                    }
                                });
                            }
                        });
                    }
                    popupMenu.showAsDropDown(v, dp2px(80), -dp2px(10));
                    return true;
                } return false;
            }
        });

        recycler.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position, RecyclerView.ViewHolder viewHolder) {
                MessageItem item = mDataList.get(position);
                EventBus.getDefault().post(new MsgFragment.UnreadMsg());
                if (v.getId() == R.id.et_item_bgaswipe_read) {
                    showProgress();
                    readone(item.getId(), new ResultCallBack<String>() {
                        @Override
                        public void onSuccess(String result) {
                            hideProgress();
                            getData();
                        }

                        @Override
                        public void onFailed(String msg) {
                            hideProgress();
                            showToast(msg);
                        }
                    });
                } else if (v.getId() == R.id.tv_item_bgaswipe_delete) {
                    showProgress();
                    deleteMsg(item.getId(), new ResultCallBack<String>() {
                        @Override
                        public void onSuccess(String result) {
                            hideProgress();
                            getData();
                        }

                        @Override
                        public void onFailed(String msg) {
                            hideProgress();
                            showToast(msg);
                        }
                    });
                } else {
                    if (type == 1) {
                        readone(item.getId(), new ResultCallBack<String>() {
                            @Override
                            public void onSuccess(String result) {
                                hideProgress();
                                getData();
                            }

                            @Override
                            public void onFailed(String msg) {
                                hideProgress();
                                showToast(msg);
                            }
                        });
                    } else if (type == 2){
                        readone(item.getId(), null);
                        ChatDetailActivity.start(getActivity(), item.getSourceUserId(),
                                item.getSourceUser().getUsername(), item.getProjectId());
                    } else if (type == 3) {
                        readone(item.getId(), null);
                        if (item.getActionParms() != null) {
                            Intent intent = new Intent(getActivity(), ProTalkDetailActivity.class);
                            intent.putExtra(BaseActivity.KEY_PROJECTID, item.getProjectId());
                            intent.putExtra(ProTalkDetailActivity.KEY_ID, item.getActionParms().getSubjectId());
                            startActivity(intent);
                        }
                    }
                }
            }
        });
    }

    /**
     * 加载搜索内容
     * @param str
     */
    public void loadData(String str) {
        searchKey = str;
        getData();
    }

    /**
     * 获取数据
     */
    private void getData() {
        if (TextUtils.isEmpty(searchKey))
            return;

        getMsgList(type, "", new ResultCallBack<MessageBundle>() {
            @Override
            public void onSuccess(MessageBundle result) {
                mDataList.clear();
                mDataList.addAll(result.getMessageItems());
                recycler.onFinishRequest(false, false);
            }

            @Override
            public void onFailed(String msg) {
                mDataList.clear();
                recycler.onFinishRequest(false, false);
            }
        });
    }
}
